/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package converters;

import beans.MonedaController;

import edu.upn.sigemfi.ope.be.Moneda;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.ConverterException;

/**
 *
 * @author Hiro
 */
public class MonedaConverter implements Converter {

    @Override
    public Object getAsObject(FacesContext arg0, UIComponent arg1, String arg2) throws ConverterException {
        if (arg2 == null) {
            return null;
        }
        try {
            Long id = Long.parseLong(arg2);
            MonedaController moc = new MonedaController();
            return moc.buscar(id);
        } catch (Exception e) {
            return null;
        }
    }

    @Override
    public String getAsString(FacesContext arg0, UIComponent arg1, Object arg2) throws ConverterException {
        if (arg2 == null) {
            return null;
        }
        if (arg2 instanceof Moneda) {
            Moneda mo = (Moneda) arg2;
            return mo.getIdMoneda().toString();
        } else {
            throw new IllegalArgumentException("El objeto que se ha pasado no es de tipo moneda");
        }
    }
}